<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="属性名" prop="attrName">
        <el-input
          v-model="queryParams.attrName"
          placeholder="请输入属性名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检索" prop="searchType">
        <el-select
          v-model="queryParams.searchType"
          placeholder="请选择是否需要检索"
          clearable
        >
          <el-option
            v-for="dict in search_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="可选值列表" prop="valueSelect">
        <el-input
          v-model="queryParams.valueSelect"
          placeholder="请输入可选值列表"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="属性类型" prop="attrType">
        <el-select v-model="queryParams.attrType" placeholder="请选择属性类型" clearable>
          <el-option
            v-for="dict in attr_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="启用状态" prop="enable">
        <el-select v-model="queryParams.enable" placeholder="请选择启用状态" clearable>
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属分类" prop="catalogId">
        <el-input
          v-model="queryParams.catalogId"
          placeholder="请输入所属分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['product:attr:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:attr:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:attr:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="属性id" align="center" prop="attrId" />
      <el-table-column label="属性名" align="center" prop="attrName" />
      <el-table-column label="检索" align="center" prop="searchType">
        <template #default="scope">
          <dict-tag :options="search_type" :value="scope.row.searchType" />
        </template>
      </el-table-column>
      <el-table-column label="可选值列表" align="center" prop="valueSelect" />
      <el-table-column label="属性类型" align="center" prop="attrType">
        <template #default="scope">
          <dict-tag :options="attr_type" :value="scope.row.attrType" />
        </template>
      </el-table-column>
      <el-table-column label="启用状态" align="center" prop="enable">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.enable" />
        </template>
      </el-table-column>
<!--      <el-table-column label="所属分类" align="center" prop="catalogId" />-->
      <el-table-column label="所属分类" align="center" prop="catName" />
      <el-table-column label="快速展示" align="center" prop="showDesc">
        <template #default="scope">
          <dict-tag :options="show_desc" :value="scope.row.showDesc" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:attr:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:attr:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品属性对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="属性名" prop="attrName">
          <el-input v-model="form.attrName" placeholder="请输入属性名" />
        </el-form-item>
        <el-form-item label="检索" prop="searchType">
          <el-select v-model="form.searchType" placeholder="请选择是否需要检索">
            <el-option
              v-for="dict in search_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="可选值列表" prop="valueSelect">
          <el-select
            v-model="form.valueSelect"
            placeholder="请选择可选值列表"
            multiple
            allow-create
            filterable
            default-first-option
          >
            <el-option
              v-for="item in attrOptions"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
          <!-- <el-input
            v-model="form.valueSelect"
            placeholder="请输入可选值列表[用逗号分隔]"
          /> -->
        </el-form-item>
        <el-form-item label="属性类型" prop="attrType">
          <el-select v-model="form.attrType" placeholder="请选择属性类型">
            <el-option
              v-for="dict in attr_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="启用状态" prop="enable">
          <el-select v-model="form.enable" placeholder="请选择启用状态">
            <el-option
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属分类" prop="catalogId">
          <el-select v-model="form.catalogId" placeholder="请选择所属分类">
            <el-option
              v-for="item in cateList"
              :key="item.catId"
              :label="item.name"
              :value="item.catId"
            >
            </el-option>
          </el-select>
          <!-- <el-input v-model="form.catalogId" placeholder="请输入所属分类" /> -->
        </el-form-item>
        <el-form-item label="快速展示" prop="showDesc">
          <el-select v-model="form.showDesc" placeholder="请选择快速展示">
            <el-option
              v-for="dict in show_desc"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  listAttr,
  getAttr,
  delAttr,
  addAttr,
  updateAttr,
} from "@/api/mall/server/product/attribute";
import { listTreeCategories } from "@/api/mall/server/product/cate";
import { reactive, ref, toRefs, getCurrentInstance } from "vue";
// vue3中获取this的方法
const { proxy } = getCurrentInstance();
const {
  search_type,
  attr_type,
  value_type,
  sys_normal_disable,
  show_desc,
} = proxy.useDict(
  "search_type",
  "attr_type",
  "value_type",
  "sys_normal_disable",
  "show_desc"
);
// 查询list
const total = ref(0);
const loading = ref(false);
const list = ref([]);
// 单选多选
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
// 对话弹窗
const title = ref("");
const open = ref(false);
// 查询参数和表单
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {},
});
const { queryParams, form, rules } = toRefs(data);
// 列表查询
const getList = async () => {
  loading.value = true;
  const res = await listAttr(queryParams.value);
  list.value = res.rows;
  // 处理valueSelect
  list.value.forEach((item) => {
    item.valueSelect = item.valueSelect.split(",");
  });
  loading.value = false;
};
// 取消
const cancel = () => {
  open.value = false;
  reset();
};

// 可选值列表
const attrOptions = ref([]);
// 表单重置
const reset = () => {
  form.value = {
    attrId: undefined,
    attrName: undefined,
    searchType: undefined,
    valueSelect: undefined,
    attrType: undefined,
    enable: undefined,
    catalogId: undefined,
    showDesc: undefined,
    valueType: undefined,
  };
};

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

// 表单重置
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};

// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.attrId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

// 添加弹窗
const handleAdd = (row) => {
  reset();
  open.value = true;
  title.value = "添加商品属性";
};
// 修改操作
const handleUpdate = async (row) => {
  reset();
  const attrId = row.attrId || ids.value;
  const res = await getAttr(attrId);
  attrOptions.value = res.data.valueSelect.split(",");
  form.value = res.data;
  form.value.valueSelect = res.data.valueSelect.split(",");
  open.value = true;
  title.value = "修改商品分类";
};
// 表单提交
const submitForm = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      // 处理valueSelect
      form.value.valueSelect = form.value.valueSelect.join(",");
      if (form.value.attrId != undefined) {
        updateAttr(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAttr(form.value).then((res) => {
          proxy.$modal.msgSuccess("添加成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};

// 删除
const handleDelete = (row) => {
  const ids = row.attrId || ids.value;
  proxy.$modal
    .confirm(`是否确认删除编号位${ids}的数据项?`)
    .then((res) => {
      return delAttr(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};

// 获取所有分类
const cateList = ref([]);
const getTreeCate = async () => {
  const res = await listTreeCategories();
  cateList.value = res.data;
};
// 初始调用
getList();
getTreeCate();
</script>
